home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9647 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: A question on for loop..
  5. Date: 12 Mar 1996 07:02:40 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4i43mgINNm3m@keats.ugrad.cs.ubc.ca>
  8. References: <4hn80a$98q@male.EBay.Sun.COM>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <4hn80a$98q@male.EBay.Sun.COM>,
  12. Murali Chari <murali@sooraj.ebay.sun.com> wrote:
  13.  >Hi,
  14.  >
  15.  >In the following piece of code:
  16.  >
  17.  >for(i=0;i< 20; i++)
  18.  >
  19.  >{
  20.  >
  21.  >
  22.  >  
  23.  >  if(condition==FALSE)
  24.  >  continue;
  25.  >
  26.  >
  27.  >
  28.  >
  29.  >} 
  30.  >
  31.  >Let us assume i=10 before it entered the loop.
  32.  
  33. What does it matter what i was before the loop? It gets initialized to zero.
  34.  
  35.  >The condition was false. So the continue statement
  36.  >got executed.
  37.  >
  38.  >Will control now go to the third statement in the for loop?
  39.  
  40. The for loop does not contain statements. It contains expressions; if it
  41. contained statements, you would be able to write compounds and things like
  42. while(...) constructs in there.
  43.  
  44.  
  45.  >i.e. will i be incremented by 1 and again tested for the condition?
  46.  
  47. Yes; ``continue'' causes the the increment step to be executed.
  48.  
  49. If you check the K&R2 appendix A, section 9.5, you will note that:
  50.  
  51.  
  52.     for ( expression1 ; expression2 ; expression 3 ) statement
  53.  
  54. is equivalent to
  55.  
  56.     expression1 ;
  57.     while ( expression2 ) {
  58.         statement
  59.         expression3 ;
  60.     }
  61.  
  62. IF the substatement does not contain a ``continue''.
  63.  
  64. With a continue (and this is not written up in K&R), the equivalent of the
  65. continue statement would be like a ``goto contin;'' in the following, except
  66. that the contin: label is just conceptual; the actual mechanism doesn't involve
  67. the use a statement label:
  68.  
  69.     expression1 ;
  70.     while ( expression2 ) {
  71.         statement
  72.     contin:
  73.         expression3 ;
  74.     }
  75. -- 
  76.  
  77.